package com.kejiguan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.kejiguan.ui.adapter.SheduleAdapter;
import com.kejiguan.ui.dialog.AddSheduleDialog;
import com.kejiguan.ui.dialog.AddSheduleDialogUtil;
import com.kejiguan.ui.dialog.CommDialogUtil;
import com.kejiguan.ui.dialog.ModifySheduleDialog;
import com.kejiguan.ui.dialog.ModifySheduleDialogUtil;
import com.kejiguan.ui.model.Shedule;
import com.kejiguan.ui.presenter.SheduleFragmentPresenter;
import com.kejiguan.ui.response.AddSheduleResponse;
import com.kejiguan.ui.response.DelSheduleResponse;
import com.kejiguan.ui.response.ModifySheduleResponse;
import com.kejiguan.ui.response.QuerySheduleResponse;
import com.kejiguan.ui.widget.MyLinearLayoutManager;
import com.kejiguan.ui.widget.SpacesItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by YYY on 2016/5/1.
 */
public class TimingScheduleSettingFragment extends Fragment implements View.OnClickListener,AddSheduleDialog.AddSheduleListerner,View.OnLongClickListener,ModifySheduleDialog.ModifySheduleListerner{


    private View rootView;
    private View addBtn;
    private RecyclerView sheduleList;
    private SheduleAdapter sheduleAdapter;
    private SheduleFragmentPresenter presenter;

    private Shedule delShedule;

    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        EventBus.getDefault().register(this);
        presenter=new SheduleFragmentPresenter();
        presenter.attach(getContext());
    }

    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {

        if (null==rootView)
        {
            rootView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_timingschedule_setting_layout, container, false);
            addBtn=rootView.findViewById(R.id.add_btn);
            sheduleList= (RecyclerView) rootView.findViewById(R.id.shedule_list);
            sheduleList.addItemDecoration(new SpacesItemDecoration(40,false));
            MyLinearLayoutManager layoutManager=new MyLinearLayoutManager(getContext());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            sheduleList.setLayoutManager(layoutManager);

            addBtn.setOnClickListener(this);
        }
        return rootView;
    }


    public void onResume()
    {
        super.onResume();
        if (sheduleAdapter==null)
        {
            presenter.queryShedules();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.add_btn:
                Intent addIntent=new Intent(getContext(),AddTimingScheduleActivity.class);
                startActivity(addIntent);
                break;
            case R.id.shedule_item:
                int position= (int) v.getTag(R.id.recyclerview_item_position);
                Shedule shedule=sheduleAdapter.getItemObject(position);

                Intent intent=new Intent(getContext(),ModifyTimingSheduleActivity.class);
                intent.putExtra("shedule",shedule);
                getContext().startActivity(intent);
                break;
            case R.id.dialog_confirm_btn:
                CommDialogUtil.getInstance().dismiss();
                presenter.delShedule(delShedule);
                break;
            case R.id.dialog_cancel_btn:
                CommDialogUtil.getInstance().dismiss();
                break;
        }
    }

    @Override
    public void addShedule(Shedule shedule) {
        presenter.addShedule(shedule);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(AddSheduleResponse response) {

        if ("0000".equals(response.getRetCode())) {

            Toast.makeText(getContext(),"新增定时任务成功",Toast.LENGTH_SHORT).show();
            presenter.queryShedules();
        }else
        {
            Toast.makeText(getContext(),response.getRetInfo(),Toast.LENGTH_SHORT).show();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(DelSheduleResponse response) {

        if ("0000".equals(response.getRetCode())) {

            Toast.makeText(getContext(),"删除定时任务成功",Toast.LENGTH_SHORT).show();
            presenter.queryShedules();
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ModifySheduleResponse response) {

        if ("0000".equals(response.getRetCode())) {

            Toast.makeText(getContext(),"修改定时任务成功",Toast.LENGTH_SHORT).show();
            presenter.queryShedules();
        }
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QuerySheduleResponse response) {

        if ("0000".equals(response.getRetCode())) {

            List<Shedule> shedules=response.getScheduleList();
            if (sheduleAdapter==null)
            {
                sheduleAdapter=new SheduleAdapter(getContext(),shedules);
                sheduleAdapter.setLongClickListener(this);
                sheduleAdapter.setOnClickListener(this);
                sheduleList.setAdapter(sheduleAdapter);

            }else
            {
                sheduleAdapter.setShedules(shedules);
            }


        }else
        {
            Toast.makeText(getContext(),response.getRetInfo(),Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public boolean onLongClick(View v) {

        switch (v.getId())
        {
            case R.id.shedule_item:
                int position= (int) v.getTag(R.id.recyclerview_item_position);
                delShedule=sheduleAdapter.getItemObject(position);
                CommDialogUtil.getInstance().show(getContext(),"要删除"+delShedule.getScheduleName()+"定时任务吗?",TimingScheduleSettingFragment.this,"删除","取消",0);
                break;
        }

        return true;
    }

    @Override
    public void modifyShedule(Shedule shedule) {
        Log.e(TimingScheduleSettingFragment.class.getSimpleName(),"modifyShedule");
        presenter.modifyShedule(shedule);

    }
}
